package com.zdw.oauth.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zdw.common.parent.JSONResult;
import com.zdw.oauth.config.international.InternationalUtils;
import com.zdw.oauth.dto.RoleAddDto;
import com.zdw.oauth.dto.RoleSelectDto;
import com.zdw.oauth.dto.RoleUpdDto;
import com.zdw.oauth.dto.UserRoleDto;
import com.zdw.oauth.service.RoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author 卓德文
 * @since 2023-07-17
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 新增角色
     *
     * @return
     */
    @ApiOperation(value = "新增角色")
    @PostMapping("/add")
    @SaCheckLogin
    @SaCheckPermission(value = "role.add", orRole = {"superAdmin", "admin", "role"})
    public JSONResult add(@RequestBody RoleAddDto dto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), roleService.add(dto));
    }

    /**
     * 编辑角色
     *
     * @return
     */
    @ApiOperation(value = "编辑角色")
    @PutMapping("/update")
    @SaCheckLogin
    @SaCheckPermission(value = "role.update", orRole = {"superAdmin", "admin", "role"})
    public JSONResult update(@RequestBody RoleUpdDto dto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), roleService.update(dto));
    }

    /**
     * 删除角色
     *
     * @return
     */
    @ApiOperation(value = "删除角色")
    @DeleteMapping("/delete")
    @SaCheckLogin
    @SaCheckPermission(value = "role.delete", orRole = {"superAdmin", "admin", "role"})
    public JSONResult delete(@RequestBody List<Integer> roleIdList) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), roleService.delete(roleIdList));
    }

    /**
     * 查询角色信息
     *
     * @return
     */
    @ApiOperation(value = "查询角色信息")
    @PostMapping("/select")
    @SaCheckPermission(value = "role.select", orRole = {"superAdmin", "admin", "role"})
    public JSONResult select(@RequestParam int id) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), roleService.select(id));
    }

    /**
     * 分页查询角色列表
     *
     * @return
     */
    @ApiOperation(value = "分页查询角色列表")
    @PostMapping("/page")
    @SaCheckPermission(value = "role.select", orRole = {"superAdmin", "admin", "role"})
    public JSONResult page(@RequestBody RoleSelectDto dto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), roleService.page(dto));
    }

    /**
     * 查询角色树
     *
     * @return
     */
    @ApiOperation(value = "查询角色树")
    @PostMapping("/tree")
    @SaCheckPermission(value = "role.select", orRole = {"superAdmin", "admin", "role"})
    public JSONResult tree() {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), roleService.tree());
    }

    /**
     * 给用户分配角色
     *
     * @return
     */
    @ApiOperation(value = "给用户分配角色")
    @PostMapping("/grantRole")
    @SaCheckPermission(value = "role.grant", orRole = {"superAdmin", "admin", "role"})
    public JSONResult grantRole(@RequestBody UserRoleDto dto) {
        return new JSONResult().markSuccess(InternationalUtils.getInternational("common.success"), roleService.grantRole(dto));
    }

}
